<?php

namespace App\Http\Controllers\Admin;

use Validator;
use App\Product;
use App\ProductType;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ProductController extends Controller
{
    public function index(Request $request)
    {
        $products = Product::orderBy('updated_at', 'desc')->get();
        return view('admin.product.index', [
            'request' => $request,
            'products' => $products
        ]);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'type' => 'required',
                'name' => 'required',
                'summary' => 'required',
                'recommend' => 'in:0,1',
                'pictures' => 'required',
                'introduce' => 'required'
            ];
            $messages = [
                'type.required' => '请选择产品分类！',
                'name.required' => '请输入产品名！',
                'summary.required' => '请输入产品摘要！',
                'recommend.in' => '请选择产品是否推荐！',
                'pictures.required' => '请上传产品图集！',
                'introduce.required' => '请输入产品介绍！'
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                $return = [
                    'code' => __LINE__,
                    'msg' => $validator->errors()->first()
                ];
                return response()->json($return);
            }
            $product = new Product();
            $product->type = $data['type'];
            $product->name = $data['name'];
            $product->summary = $data['summary'];
            $product->recommend = $data['recommend'];
            $product->pictures = rtrim($data['pictures'], ',');
            $product->introduce = $data['introduce'];
            if ($product->save()) {
                $return = ['code' => 0];
            } else {
                $return = [
                    'code' => __LINE__,
                    'msg' => '新增产品失败，请检查输入或请稍候重试！'
                ];
            }
            return response()->json($return);
        }
        $productTypes = ProductType::orderBy('created_at', 'desc')->get();
        return view('admin.product.add', [
            'request' => $request,
            'productTypes' => $productTypes
        ]);
    }

    public function edit(Request $request, $id)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'type' => 'required',
                'name' => 'required',
                'summary' => 'required',
                'recommend' => 'in:0,1',
                'introduce' => 'required'
            ];
            $messages = [
                'type.required' => '请选择产品分类！',
                'name.required' => '请输入产品名！',
                'summary.required' => '请输入产品摘要！',
                'recommend.in' => '请选择产品是否推荐！',
                'introduce.required' => '请输入产品介绍！'
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                $return = [
                    'code' => __LINE__,
                    'msg' => $validator->errors()->first()
                ];
                return response()->json($return);
            }
            $product = Product::find($data['id']);
            $product->type = $data['type'];
            $product->name = $data['name'];
            $product->summary = $data['summary'];
            $product->recommend = $data['recommend'];
            if (!empty($data['pictures'])) {
                $product->pictures = rtrim($data['pictures'], ',');
            }
            $product->introduce = $data['introduce'];
            if ($product->save()) {
                $return = ['code' => 0];
            } else {
                $return = [
                    'code' => __LINE__,
                    'msg' => '修改产品失败，请检查输入或请稍候重试！'
                ];
            }
            return response()->json($return);
        }
        $productTypes = ProductType::orderBy('created_at', 'desc')->get();
        $product = Product::find($id);
        return view('admin.product.edit', [
            'request' => $request,
            'product' => $product,
            'productTypes' => $productTypes
        ]);
    }

    public function del(Request $request)
    {
        $id = $request->input('id');
        $product = Product::find($id);
        if (!empty($product->pictures)) {
            $pictureArr = explode(',', $product->pictures);
            foreach ($pictureArr as $fileName) {
                $file = 'upload/' . iconv('utf-8', 'gbk', $fileName);
                if (is_file($file)) {
                    unlink($file);
                }
            }
        }
        if ($product->delete()) {
            $return = ['code' => 0];
        } else {
            $return = [
                'code' => __LINE__,
                'msg' => '删除失败,请稍候重试！'
            ];
        }
        return response()->json($return);
    }
}